#include "app.h"
#include "platform.h"
#include "io.h"
#include "pid.h"

#include "lcd.h"
#include "flash.h"
#include "sdcard.h"
#include "malloc.h"
#include "i2c_app.h"

#include "lv_user.h"
#include "demos/lv_demos.h"

#include "ff.h"
#include "lua_if.h"

#include "stdio.h"

__IO uint16_t adc_avg_arr[ADC_CHANNEL_NUM];

__IO system_obj sys_cur_para;

const char LUA_SCRIPT_GLOBAL[] =
"                               \
off = 1500                      \
on = 500                        \
while 1 do                      \
	led(0)                      \
	delayms(off)                \
	led(1)                      \
	delayms(on)                 \
end";

const uint16_t dac_fast_vol_tab[FAST_VOL_NUM]=
{
    608,    //1.5V
    624,    //1.8V
    663,    //2.5V
    689,    //3V
    705,    //3.3V
    722,    //3.6V
    743,    //4V
    799,    //5V
    852,    //6V
    1017,   //9V
    1178,   //12V
    1342,   //15V
    1505,   //18V
    1615,   //20V
    1831,   //24V
    2048,   //28V
    2265,   //32V
    2481,   //36V
    3139,   //48V
    3792,   //60V
    4095,   //MAX
};

const static user_para  user_def_para=
{
    .language = SYS_PARA_LANGUARE_CH,
    .software_ver = SOFTWARE_VERSION,
    .hardware_ver = HARDWARE_VERSION,
    .lcd_bg_pwm = 80,                      //full 100
    .lcd_slp_tim = 0,
    
    .vol_set = 800,
    .cur_set = 0,
    .adc_scale_para[0] = 1,
    .adc_scale_para[1] = 1,
    .adc_scale_para[2] = 1,
    .adc_scale_para[3] = 1,
    .adc_scale_para[4] = 1,
    .adc_scale_para[5] = 1,
    .adc_scale_para[6] = 1,
    .adc_scale_para[7] = 1,
    
    .adc_input_1v5 = 38,
    .adc_input_60v = 1551,
    .adc_input_01a = 19,
    .adc_input_2a = 372,
    .adc_output_1v5 = 38,
    .adc_output_60v = 1551,
    .adc_output_01a = 19,
    .adc_output_2a = 372,
    
    .dac_fast_vol[0] = 608,
    .dac_fast_vol[1] = 624,
    .dac_fast_vol[2] = 663,
    .dac_fast_vol[3] = 689,
    .dac_fast_vol[4] = 705,
    .dac_fast_vol[5] = 722,
    .dac_fast_vol[6] = 743,
    .dac_fast_vol[7] = 799,
    .dac_fast_vol[8] = 852,
    .dac_fast_vol[9] = 1017,
    .dac_fast_vol[10] = 1178,
    .dac_fast_vol[11] = 1342,
    .dac_fast_vol[12] = 1505,
    .dac_fast_vol[13] = 1615,
    .dac_fast_vol[14] = 1831,
    .dac_fast_vol[15] = 2048,
    .dac_fast_vol[16] = 2265,
    .dac_fast_vol[17] = 2481,
    .dac_fast_vol[18] = 3139,
    .dac_fast_vol[19] = 3792,
    .dac_fast_vol[20] = 4095,
};

void Rom_Save_User_Set(user_para * para)
{
    int i, j;
    
    uint16_t rom_buf[USER_PARA_FLASH_SIZE*2];
    j = 0;

    rom_buf[j++] = EEDATA_FLAG1;
    rom_buf[j++] = EEDATA_FLAG2;
    rom_buf[j++] = para->language;
    rom_buf[j++] = para->software_ver;
    rom_buf[j++] = para->hardware_ver;
    rom_buf[j++] = para->lcd_bg_pwm;
    rom_buf[j++] = para->lcd_slp_tim;
    rom_buf[j++] = para->vol_set;
    rom_buf[j++] = para->cur_set;
    rom_buf[j++] = para->adc_input_1v5;
    rom_buf[j++] = para->adc_input_60v;
    rom_buf[j++] = para->adc_input_01a;
    rom_buf[j++] = para->adc_input_2a;
    rom_buf[j++] = para->adc_output_1v5;
    rom_buf[j++] = para->adc_output_60v;
    rom_buf[j++] = para->adc_output_01a;
    rom_buf[j++] = para->adc_output_2a;

    for(i=0;i<FAST_VOL_NUM;i++)
    {
        rom_buf[j++] = para->dac_fast_vol[i];
    }
    for(i=0;i<ADC_SCALE_PARAS_NUM * 2;i++)
    {
        rom_buf[j++] = para->adc_scale_para[i];
    }
    
    flash_write(USER_PARA_SAVE_ADDR, rom_buf, USER_PARA_FLASH_SIZE*2);
    printf("write data:\r\n");
    for(i=0;i<USER_PARA_FLASH_SIZE;i++)
    {
        printf("0x%04X ",rom_buf[i]);
    }
    printf("\r\n ");
    printf("save para ok!\r\n");
}

void Rom_Para_Init(user_para * para)
{
    uint8_t i;
    uint16_t rom_buf[USER_PARA_FLASH_SIZE*2];
    flash_read(USER_PARA_SAVE_ADDR, rom_buf, USER_PARA_FLASH_SIZE);
    printf("read data:\r\n");
    for(i=0;i<USER_PARA_FLASH_SIZE;i++)
    {
        printf("0x%04X ",rom_buf[i]);
    }
    printf("\r\n ");
    if((rom_buf[0] == EEDATA_FLAG1) && (rom_buf[1] == EEDATA_FLAG2))
    {
        para->language = rom_buf[2];
        para->software_ver = rom_buf[3];
        para->hardware_ver = rom_buf[4];
        para->lcd_bg_pwm = rom_buf[5];
        para->lcd_slp_tim = rom_buf[6];
        para->vol_set = rom_buf[7];
        para->cur_set = rom_buf[8];
        
        para->adc_input_1v5 = rom_buf[9];
        para->adc_input_60v = rom_buf[10];
        para->adc_input_01a = rom_buf[11];
        para->adc_input_2a = rom_buf[12];
        para->adc_output_1v5 = rom_buf[13];
        para->adc_output_60v = rom_buf[14];
        para->adc_output_01a = rom_buf[15];
        para->adc_output_2a = rom_buf[16];
        for(i=0;i<FAST_VOL_NUM;i++)
        {
            para->dac_fast_vol[i] = rom_buf[17 + i];
        }
        for(i=0;i<ADC_SCALE_PARAS_NUM * 2;i++)
        {
            para->adc_scale_para[i] = rom_buf[17 + FAST_VOL_NUM + i];
        }
        if(1)
        {
            printf("sys language:%d.\r\n",      para->language);
            printf("sys software_ver:%d.\r\n",  para->software_ver);
            printf("sys hardware_ver:%d.\r\n",  para->hardware_ver);
            printf("sys lcd_bg_pwm:%d.\r\n",    para->lcd_bg_pwm);
            printf("sys lcd_slp_tim:%d.\r\n",   para->lcd_slp_tim);
            printf("sys vol_set:%d.\r\n",       para->vol_set);
            printf("sys cur_set:%d.\r\n",       para->cur_set);
            
            printf("sys adc_input_1v5:%d.\r\n", para->adc_input_1v5);
            printf("sys adc_input_60v:%d.\r\n", para->adc_input_60v);
            printf("sys adc_input_01a:%d.\r\n", para->adc_input_01a);
            printf("sys adc_input_2a:%d.\r\n",  para->adc_input_2a);
            printf("sys adc_output_1v5:%d.\r\n",    para->adc_output_1v5);
            printf("sys adc_output_60v:%d.\r\n",    para->adc_output_60v);
            printf("sys adc_output_01a:%d.\r\n",    para->adc_output_01a);
            printf("sys adc_output_2a:%d.\r\n",     para->adc_output_2a);
            
            for(i=0;i<FAST_VOL_NUM;i++)
            {
                printf("dac_fast_vol%d:%d\r\n", i, para->dac_fast_vol[i]);
            }
            
            for(i=0;i<ADC_SCALE_PARAS_NUM;i++)
            {
                printf("scale%d:%.3f\r\n", i, (float)para->adc_scale_para[i*2]/(float)para->adc_scale_para[i*2+1]);
            }
        }
    }
    else
    {
        printf("save default para\r\n");
        Rom_Save_User_Set((user_para *)&user_def_para);
    }
}

void system_obj_init(system_obj *obj)
{
    //系统状态参数，不保存，掉电清零
    obj->run_time = 0;
    obj->temp = 0;          //温度
    obj->input_vol = 0;
    obj->input_cur = 0;
    obj->input_pow = 0;
    obj->output_vol = 0;
    obj->output_cur = 0;
    obj->output_pow = 0;
    
    obj->pow_out_en = 0;    //开机关闭输出
    obj->fast_vol_sel = 7;  //5V
    obj->xiaolv = 100;
    obj->para_save_flag = 0;
}

/*  log
[Rx] system run
[Rx] read data:
0x9420 0x5210 0x0000 0x0000 0x0002 0x0003 0x00C8 0x0000 0x0000 0x0000 0x0000 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 
 save default para
[Rx] write data:
0x9420 0x5211 0x0000 0x0002 0x0003 0x00C8 0x0000 0x0000 0x0000 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 
 save para ok!
id:0xef17
[Rx] sdcard type:0
[Rx] flash fatfs init ok
[Rx] system run
[Rx] read data:
0x9420 0x5211 0x0000 0x0002 0x0003 0x00C8 0x0000 0x0000 0x0000 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 0x0001 
 sys language:0.
sys software_ver:2.
sys hardware_ver:3.
sys lcd_bg_pwm:200.
sys lcd_slp_tim:0.
sys vol_set:0.
sys cur_set:0.
scale0:1.000
scale1:1.000
scale2:1.000
scale3:1.000
id:0xef17
[Rx] sdcard type:0
[Rx] flash fatfs init ok
*/

system_obj sys_def_para;

__IO uint32_t uwTick = 0;
__IO uint32_t Timer_Task_Cnt[TASK_NUM] = {0,0,0,0,0};
__IO uint32_t Timer_Task_Tick[TASK_NUM] = {5,10,25,500,1};

void HAL_IncTick(void)
{
    int i;
    uwTick += 10;
    for(i = 0;i < TASK_NUM;i ++)
    {
        if(Timer_Task_Cnt[i] > 0)
        {
            Timer_Task_Cnt[i] --;
        }
    }
    Button_handle();
}

uint32_t HAL_TickGet(void)
{
    return uwTick;
}

#include "hxcmod.h"

#define PLAYER_SAMPLE_RATE 96000
// Mod player context
modcontext mcontext;
// MOD data
extern const unsigned char mod_data[39424];
// Computed sound output buffer
#define SAMPLE_BUFFER_SIZE 8192
msample dmasoundbuffer[SAMPLE_BUFFER_SIZE] __attribute__ ((aligned (4)));

void main_init(void)
{
    uint32_t i;
    nvic_priority_group_config(NVIC_PRIORITY_GROUP_2);
    system_clock_config();
    delay_init();

    uart_print_init(115200);
    printf("system run\r\n");

    io_drv_init();
    adc_drv_init();
    dac_drv_init();
    spi_drv_init();
    tmr_drv_init();
    LCD_Init();
    Key_Init();

    Rom_Para_Init((user_para *)&sys_cur_para.user_set);
    system_obj_init((system_obj *)&sys_cur_para);
    
    /* usb gpio config */
    usb_config();
    SPIFlash_Init();
    SD_Initialize();
    printf("sdcard type:%d\r\n",sd_type);
    my_mem_init(SRAMIN);
    fatfs_init();
    lv_user_init();

    lua_Init();
    lua_RegisterFunc();
    //lua_do((char *)LUA_SCRIPT_GLOBAL);
    dac_software_trigger_generate(DAC1_SELECT);
    dac_1_data_set(DAC1_12BIT_RIGHT,4095);
    
    dac_software_trigger_generate(DAC2_SELECT);
    dac_2_data_set(DAC2_12BIT_RIGHT,4095);
    
    EG1151_EN();
}

extern __IO uint16_t adc1_ordinary_valuetab[];
extern void ui_page_ctr(void);

void main_loop(void)
{
    while(1)
    {
        //50ms
        if(!Timer_Task_Cnt[0])
        {
            Timer_Task_Cnt[0] = Timer_Task_Tick[0];
            if(FUNC_KEY_GET() == CODE_CLICK)
            {
            }
            else if (FUNC_KEY_GET() == CODE_D_CLICK)
            {
            }
            else if (FUNC_KEY_GET() == CODE_L_CLICK)
            {
            }
            else if (FUNC_KEY_GET() == CODE_T_CLICK)
            {
            }
            FUNC_KEY_CLEAR();
        }
        //100ms
        if(!Timer_Task_Cnt[1])
        {
            Timer_Task_Cnt[1] = Timer_Task_Tick[1];
        }
        //250//
        if(!Timer_Task_Cnt[2])
        {
            Timer_Task_Cnt[2] = Timer_Task_Tick[2];
        }
        //5000ms
        if(!Timer_Task_Cnt[3])
        {
            Timer_Task_Cnt[3] = Timer_Task_Tick[3];
            if(sys_cur_para.para_save_flag)
            {
                Rom_Save_User_Set((user_para *)&sys_cur_para.user_set);
                sys_cur_para.para_save_flag = 0;
            }
            LED_TOG();
        }
        //10ms
        if(!Timer_Task_Cnt[4])
        {
            Timer_Task_Cnt[4] = Timer_Task_Tick[4];
            if(sys_cur_para.pow_out_en)
            {
                dac_handler(4095 - sys_cur_para.user_set.vol_set);
            }
            else
            {
                dac_handler(4095);
            }
        }
        
        usbh_loop_handler(&otg_core_struct.host);
        lv_task_handler();
        
//        dac_software_trigger_generate(DAC2_SELECT);
//        dac_2_data_set(DAC2_12BIT_RIGHT,(lv_tick_get()*5)%4096);
        if(0)
        printf("adc1:%5d,%5d,%5d,%5d\r\n",\
               adc_avg_arr[0],adc_avg_arr[1],\
               adc_avg_arr[2],adc_avg_arr[3]);
    }
}

void dac_handler(uint16_t para)
{
    dac_software_trigger_generate(DAC1_SELECT);
    dac_1_data_set(DAC1_12BIT_RIGHT,para);
}

void pwm_handler(uint16_t pwm)
{
    tmr_channel_value_set(TMR5, TMR_SELECT_CHANNEL_1, pwm);
}

void adc_dma_handler(void)
{
    //87.7us
    uint16_t i;
    uint32_t sum[ADC_CHANNEL_NUM];

    for(i=0;i<ADC_CHANNEL_NUM;i++)sum[i] = 0;
    for(i=0;i<ADC_FILT_NUM;i++)
    {
        sum[0] += adc1_ordinary_valuetab[ADC_CHANNEL_NUM * i + 0];
        sum[1] += adc1_ordinary_valuetab[ADC_CHANNEL_NUM * i + 1];
        sum[2] += adc1_ordinary_valuetab[ADC_CHANNEL_NUM * i + 2];
        sum[3] += adc1_ordinary_valuetab[ADC_CHANNEL_NUM * i + 3];
    }
    for(i=0;i<ADC_CHANNEL_NUM;i++)
    {
        sum[i] /= ADC_FILT_NUM;
        adc_avg_arr[i] = sum[i];
    }
}

FATFS mFatfs[1];
BYTE work[FF_MAX_SS];

void fatfs_init(void)
{
    FRESULT res;
    res = f_mount(&mFatfs[0],"0:",1);
    if(res == FR_NO_FILESYSTEM)
    {
        printf("flash fatfs init error,no filesystem\r\n");
        res = f_mkfs("0:", 0, work, sizeof(work));
        if(res!=FR_OK)
        {
            printf("flash fatfs create error\r\n");
            return ;
        }
        else
        {
            printf("flash fatfs create sucess\r\n");
        }
    }
    else if(res == FR_OK)
    {
        printf("flash fatfs init ok\r\n");
    }
    res = f_mount(NULL,"0:",1);
    res = f_mount(&mFatfs[0],"0:",1);
    if(res)
    {
        printf("flash mount err:%d.\r\n", res);
    }

    if(sd_type != SD_TYPE_ERR)
    {
        printf("sd card insert\r\n");
        res = f_mount(&mFatfs[0],"1:",1);
        if(res == FR_NO_FILESYSTEM)
        {
            printf("sdcard fatfs init error,no filesystem\r\n");
            res = f_mkfs("1:", 0, work, sizeof(work));
            if(res!=FR_OK)
            {
                printf("sdcard fatfs create error\r\n");
                return ;
            }
            else
            {
                printf("sdcard fatfs create sucess\r\n");
            }
        }
        else if(res == FR_OK)
        {
            printf("sdcard fatfs init ok\r\n");
        }
        res = f_mount(NULL,"1:",1);
        res = f_mount(&mFatfs[0],"1:",1);
        if(res)
        {
            printf("sdcard mount err:%d.\r\n", res);
        }
        else if(1)
        {
            Write_TXT_File("1:log.txt","system startup",0x80);
            //Read_TXT_File("1:log.txt");
            //Write_TXT_File("1:main.lua",(char *)LUA_SCRIPT_GLOBAL,0x00);
            Read_TXT_File("1:main.lua");
        }
    }
}

void Write_TXT_File(char *filename,char *str,uint8_t mode)
{
    FIL f_txt;      //文件
    DIR dir;
    UINT br;
    char * file;
    uint8_t res = 0;
    file = filename;

    res=f_open(&f_txt,(const TCHAR*)file,FA_READ|FA_WRITE);     //以读方式打开文件
    if(res == FR_NO_FILE)
    {
        res=f_open(&f_txt,(const TCHAR*)file,FA_READ|FA_WRITE|FA_CREATE_NEW);   //创建新的文件
    }
    if(res == FR_OK)
    {
        f_lseek(&f_txt,f_txt.obj.objsize);              //指向文件的末尾
        if(mode & 0x80)
        {
            f_write(&f_txt,"\r\n",strlen((const char*)"\r\n"),(UINT*)&br);
        }
        f_write(&f_txt,str,strlen((const char*)str),(UINT*)&br);    //写入内容
    }
    f_close(&f_txt);

    res=f_opendir(&dir,(const TCHAR*)DEFAULT_DIR);
    if(res == FR_NO_PATH)
    {
        f_closedir(&dir);
        res=f_mkdir((const TCHAR*)DEFAULT_DIR);   //创建新的文件夹
    }
    f_closedir(&dir);
    res=f_opendir(&dir,(const TCHAR*)DEFAULT_LUA_DIR);
    if(res == FR_NO_PATH)
    {
        f_closedir(&dir);
        res=f_mkdir((const TCHAR*)DEFAULT_LUA_DIR);   //创建新的LUA文件夹
    }
    f_closedir(&dir);
}

uint8_t Read_TXT_File(char *filename)
{
    FIL f_txt;      //文件
    DIR dir;
    UINT br;
    uint8_t res = 0;
    uint8_t txt_buf[4096];

//    res=f_opendir(&dir,(const TCHAR*)filename);
//    if(res == FR_OK)
    {
        res=f_open(&f_txt,(const TCHAR*)filename,FA_READ);     //以读方式打开文件
        if(res == FR_NO_FILE)
        {
            printf("not find TXT file\r\n");
        }
        else if(res == FR_OK)
        {
            f_read(&f_txt,txt_buf,f_txt.obj.objsize,&br);
            f_close(&f_txt);
            if(br != 0)
            {
                printf("txt:%s\r\n",txt_buf);
            }
        }
    }
    f_close(&f_txt);
//    f_closedir(&dir);
    return res;
}

static int lua_if_led(lua_State* L)
{
    uint8_t para;
    if (lua_type(L, 1) == LUA_TNUMBER)
    {
        para = luaL_checknumber(L, 1);
        if(para == 1)
        {
            LED_ON();
        }
        else if(para == 0)
        {
            LED_OFF();
        }
    }
    return 0;
}

static int delayms(lua_State* L)
{
    uint32_t para;
    if (lua_type(L, 1) == LUA_TNUMBER)
    {
        para = luaL_checknumber(L, 1);
        delay_ms(para);
    }
    return 0;
}

static int get_led(lua_State* L)
{
    int32_t led;
    led = LED_IS_ON();
    lua_pushnumber(L, led);
    return 1;
}

void lua_RegisterFunc(void)
{
    //将指定的函数注册为Lua的全局函数变量，其中第一个字符串参数为Lua代码
    //在调用C函数时使用的全局函数名，第二个参数为实际C函数的指针。
    lua_register(g_Lua, "led",      lua_if_led);
    lua_register(g_Lua, "delayms",  delayms);
    lua_register(g_Lua, "get_led",  get_led);
}

// 重新封装下执行函数，支持打印调试信息
void lua_do(char *buf)
{
    int re;
    static const char *str;
    re = luaL_dostring(g_Lua, buf);
    if (re != LUA_OK)
    {
        str = lua_tostring(g_Lua, -1);
        if (strstr(str, "attempt to yield from outside a coroutine"))   /* 用户终止了程序 */
        {
            /* 程序被用户终止\r\n 这是UTF8编码,需要GBK编码 */
            printf("\xB3\xCC\xD0\xF2\xB1\xBB\xD3\xC3\xBB\xA7\xD6\xD5\xD6\xB9\r\n");
        }
        else    /* 程序语法或执行错误 */
        {
            printf("%s\r\n",str);
        }
        lua_pop(g_Lua, 1); //将错误信息出栈
    }
}

uint8_t Run_LUA_Exe(void)
{
    FIL f_txt;      //文件
    DIR dir;
    UINT br;
    uint8_t res = 0;
    char * lua_buf;
    res=f_opendir(&dir,(const TCHAR*)DEFAULT_LUA_FILE_PATH);
    if(res == FR_OK)
    {
        res=f_open(&f_txt,(const TCHAR*)DEFAULT_LUA_FILE_PATH"/"DEFAULT_LUA_FILE_NAME,FA_READ);     //以读方式打开文件
        if(res == FR_NO_FILE)
        {
            printf("Lua文件不存在");
        }
        else if(res == FR_OK)
        {
            lua_buf = mymalloc(SRAMIN,f_txt.obj.objsize);
            if(lua_buf == NULL)
            {
                printf("内存不足了哦!");
            }
            else
            {
                f_read(&f_txt,lua_buf,f_txt.obj.objsize,&br);
                f_close(&f_txt);
                if(br != 0)
                {
                    lua_do((char *)lua_buf);
                }
                else
                {
                    printf("空LUA文件哦!");
                }
            }
            myfree(SRAMIN,lua_buf);
        }
    }
    f_close(&f_txt);
    f_closedir(&dir);
    return res;
}

const uint16_t NTC_TABLE[191]=
{
    // *10 res
    //-40~-1
    33660,31500,29500,27640,25900,
    24280,22780,21380,20060,18840,
    17700,16640,15650,14730,13850,
    13040,12290,11580,10910,10290,
    9712,9166,8654,8172,7722,
    7298,6900,6526,6176,5846,
    5534,5242,4966,4708,4464,
    4234,4016,3812,3620,3438,
    //0~39
    3266,3104,2950,2806,2668,
    2540,2418,2302,2192,2088,
    1990,1897,1809,1726,1646,
    1571,1500,1432,1368,1307,
    1249,1194,1142,1092,1045,
    1000,9574,9166,8778,8408,
    8058,7722,7404,7098,6808,
    6532,6268,6016,5776,5546,
    //40~79
    5326,5118,4918,4726,4544,
    4368,4202,4042,3888,3742,
    3602,3468,3340,3216,3098,
    2986,2878,2774,2674,2580,
    2488,2400,2316,2234,2158,
    2082,2012,1942,1876,1813,
    1751,1693,1637,1582,1530,
    1480,1432,1385,1341,1298,
    //80~119
    1256,1216,1178,1141,1105,
    1071,1038,1006,975,9452,
    9164,8888,8620,8364,8114,
    7874,7642,7418,7202,6994,
    6792,6596,6408,6226,6050,
    5880,5714,5556,5402,5252,
    5108,4968,4832,4702,4574,
    4452,4334,4218,4106,3998,
    //120~150
    3894,3792,3694,3598,3506,
    3416,3328,3244,3162,3082,
    3006,2930,2858,2788,2720,
    2652,2588,2526,2464,2406,
    2348,2292,2238,2184,2134,
    2084,2036,1988,1942,1897,
    1854
};

uint32_t ntc_tab_get(uint8_t i)
{
    if(i < 66)
    {
        return NTC_TABLE[i] * 100;
    }
    else if(i < 129)
    {
        return NTC_TABLE[i] * 10;
    }
    else if(i < 190)
    {
        return NTC_TABLE[i];
    }
}

void ntc_temp_get(uint32_t ntc_res,int16_t * temp)
{
    uint8_t i;
    uint32_t res = ntc_res;
    if(res >= ntc_tab_get(0))
    {
        * temp = -400;
        return;
    }
    else if(res <= ntc_tab_get(190))
    {
        * temp = 1500;
        return;
    }
    for(i = 0;i < 190;i++)
    {
        if(res == ntc_tab_get(i))
        {
            * temp = (i - 40) * 10;
        }
        else if((res < ntc_tab_get(i)) && (res > ntc_tab_get(i + 1)))
        {
            * temp = (i - 39) * 10 + (10 -((res - ntc_tab_get(i + 1)) * 10 / (ntc_tab_get(i) - ntc_tab_get(i + 1))));
        }
    }
}
